﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Utility
{
   public class GeneralMethod
   {
        /// <summary>
        /// 用泛型反射将DataTable转换为实体
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="table">dt</param>
        /// <returns></returns>
        public static List<T> DataToList<T>(DataTable table)
        {
            List<T> list = new List<T>();
            T t = default(T);
            PropertyInfo[] propertypes = null;
            string tempName = string.Empty;
            foreach (DataRow row in table.Rows)
            {
                t = Activator.CreateInstance<T>();
                propertypes = t.GetType().GetProperties();
                foreach (PropertyInfo pro in propertypes)
                {
                    tempName = pro.Name;
                    if (table.Columns.Contains(tempName))
                    {
                        object value = DBNull.Value.Equals(row[tempName]) ? null : row[tempName];
                        pro.SetValue(t, value, null);
                    }
                }
                list.Add(t);
            }
            return list;
        }

        /// <summary>
        ///  序列化 str
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson<T>(T obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        /// <summary>
        /// 反序列化 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonstr"></param>
        /// <returns></returns>
        public static T JsonReverse<T>(string jsonstr)
        {
            return JsonConvert.DeserializeObject<T>(jsonstr);
            
        }

        public static JObject JsonJObject(string jsonstr)
        {
            return JObject.Parse(jsonstr);
        }

        /// <summary>
        /// Request
        /// </summary>
        /// <returns></returns>
        //public static string Request()
        //{
        //    string pamet = HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString.ToString(), Encoding.UTF8);
        //    if (string.IsNullOrEmpty(pamet))
        //        pamet = HttpContext.Current.Request["inParamJson"];
        //    if (string.IsNullOrEmpty(pamet))
        //        pamet = HttpContext.Current.Request["parm"];

        //    if (string.IsNullOrEmpty(pamet))
        //    {
        //        Stream reqStream = HttpContext.Current.Request.InputStream;
        //        reqStream.Seek(0, SeekOrigin.Begin);
        //        byte[] buffer = new byte[(int)reqStream.Length];
        //        reqStream.Read(buffer, 0, (int)reqStream.Length);
        //        string json = string.Empty;
        //        foreach (char a in buffer) { json = json + a.ToString(); }
        //        pamet = HttpUtility.UrlDecode(json.ToString(), Encoding.UTF8);
        //    }
        //    return pamet;
        //}

        #region 时间戳
        /// <summary>
		/// 时间戳转为C#格式时间
		/// </summary>
		/// <param name=”timeStamp”></param>
		/// <returns></returns>
		public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name=”time”></param>
        /// <returns></returns>
        public static string ConvertDateTime(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return Convert.ToString((int)(time - startTime).TotalSeconds);
        }

        /// <summary>
        /// 获取以0点0分0秒开始的日期
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static DateTime GetStartDateTime(DateTime d)
        {
            if (d.Hour != 0)
            {
                var year = d.Year;
                var month = d.Month;
                var day = d.Day;
                var hour = "0";
                var minute = "0";
                var second = "0";
                d = Convert.ToDateTime(string.Format("{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minute, second));
            }
            return d;
        }

        /// <summary>
        /// 获取以23点59分59秒结束的日期
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static DateTime GetEndDateTime(DateTime d)
        {
            if (d.Hour != 23)
            {
                var year = d.Year;
                var month = d.Month;
                var day = d.Day;
                var hour = "23";
                var minute = "59";
                var second = "59";
                d = Convert.ToDateTime(string.Format("{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minute, second));
            }
            return d;
        }
        #endregion

    }
}
